package com.zyl.points;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zyl.points.bean.Question;
import com.zyl.points.bean.Task;
import com.zyl.points.service.QuestionService;

public class Study extends Activity {
//    private int rightIndex;
    private Boolean right=false;
    private Question current ;
    private List<Question> options ;
	private Task task ;
	private QuestionService questionService;
	   TextView  title =   null;
	   TextView  prompt =  null;
	   TextView  answerView = null;
	   TextView  option2 = null;
	   
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.study);
       questionService =new QuestionService(Study.this); 
       task=Task.getInstance(Study.this);
       
      
       initText(); 
    }

	private void initText() {
	       title = (TextView)findViewById(R.id.title); 
		   prompt = (TextView)findViewById(R.id.prompt); 
		   answerView = (TextView)findViewById(R.id.answer); 
		   
		   //初始化 qid
		   task.setQid(task.getQid()-1);
		   task.getCurrent().setId(task.getCurrent().getId()-1);
		   
		   List<Question>  options = questionService.nextList();
		   if(options==null) {
			   title.setText("您已完成所有题目");
			   return;
		   }  
		   current =task.getCurrent();
		   title.setText(current.getTitle());
		   answerView.setText("");  
		 
		  
	}
    
    
    public void show(View view){  
    	questionService.addAppearTimes(task.getQid());
    	 answerView.setText(current.getAnswer());  
    	
    }
    public void yes(View view){  
    	right=true;
    	nextQuesiton(title, prompt, answerView  ); 
    	
    }
    public void no(View view){  
		questionService.addAppearTimes(task.getQid());
   	    answerView.setText(current.getAnswer());  
    	
    }
   
    public void next(View view){  
    	right=null;
    	nextQuesiton(title, prompt, answerView  ); 
    	
    }
    
    
    private void nextQuesiton(final TextView title,
			final TextView prompt, final TextView answerView  ) {
    	    if(right!=null)
    	    if(right==true){
			questionService.reduceAppearTimes(task.getQid());
			}else{
			questionService.addAppearTimes(task.getQid());
			}
	 
		 options=questionService.nextList();
		
		 if(options==null){
		    
			 if(right){
					 
					title.setText(" 已完成所有题目!");
					}
			   prompt.setText("");
			   answerView.setText("");  
			   
			    
			return;
			
		}
		 if(task.getFinsh()!=null){
			 if( task.getFinsh() ){
				 //task.setPage(task.getPage()+1);
				 task.setFinsh(false);
				 // new TaskService(Test.this).update(task) ;
//				 Toast toast = Toast.makeText(getApplicationContext(), "本组题目已完成下一组为:"+ task.getCurrent().getTitle(), Toast.LENGTH_LONG);
//				 toast.show();
				 Dialog alertDialog = new AlertDialog.Builder(Study.this). 
	                setTitle("本组题目已完成"). 
	                setMessage("是否进入下一组:"+task.getCurrent().getTitle()). 
	                setPositiveButton("确定", new DialogInterface.OnClickListener() { 
	                    @Override 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	 
	                    } 
	                }). 
	                setNegativeButton("取消", new DialogInterface.OnClickListener() { 
	                    public void onClick(DialogInterface dialog, int which) { 
	                    	Intent intent = new Intent();  
	          				 intent.setClass(Study.this,Main.class);  
	          				Study.this.startActivity(intent);  
	                    } 
	                }).create(); 
	        alertDialog.show(); 
				 
			 }
		 }
			String star="";
			for(int i=0;i< current.getAppearTimes();i++){
				star=star+"☆";
			}
			 
			prompt.setText(star );
			 
			
		   current =task.getCurrent();
		   title.setText(current.getTitle());
		   answerView.setText("");  
		   
	}
}